/*
 * 文件名：StopBuildController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月30日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.ic.web;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.ic.domain.BuildInfoDomain;
import top.infopub.mgr.ic.domain.StopBuildAssist;
import top.infopub.mgr.ic.domain.StopBuildDomain;
import top.infopub.mgr.ic.service.BuildInfoService;
import top.infopub.mgr.ic.service.StartBuildService;
import top.infopub.mgr.ic.service.StopBuildService;
import top.infopub.mgr.ic.vo.StartBuildVo;
import top.infopub.mgr.ic.vo.StopBuildVo;
import top.infopub.mgr.sg.service.CoopInfoService;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.hr.StaffInfo;
import top.infopub.model.ic.BuildInfo;
import top.infopub.model.ic.StartBuild;
import top.infopub.model.sg.CoopInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;


/**
 * 停工审批
 * @author WuZhiyang
 * @version 2020年3月30日
 * @see StopBuildController
 * @since
 */
@Controller
@RequestMapping("/stopBuild")
public class StopBuildController extends GenericFlowController {

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private StopBuildService stopBuildService;

    @Autowired
    private BuildInfoService buildInfoService;

    @Autowired
    private StartBuildService startBuildService;
    
    @Autowired
    private CoopInfoService coopInfoService;

    /**
     * 停工审批列表
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "ic/lay-stopBuild-list";
    }

    /**
     * 停工审批列表数据
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<StopBuildDomain> queryDataByCondition(StopBuildVo stopBuildVo, ParamPage paramPage) {
        Page<StopBuildDomain> page = new Page<StopBuildDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (stopBuildVo.getEndDate() != null) {
            stopBuildVo.setEndDate(DateUtils.addDays(stopBuildVo.getEndDate(), 1));
        }
        //查询时，结束日期增加一天
        if (stopBuildVo.getPlanEndDate() != null) {
            stopBuildVo.setPlanEndDate(DateUtils.addDays(stopBuildVo.getPlanEndDate(), 1));
        }
        page = stopBuildService.selectByDomainWhere(page, stopBuildVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 停工审批-我审批的列表数据
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<StopBuildDomain> queryMyCheckDataByCondition(StopBuildVo stopBuildVo,
                                                              ParamPage paramPage) {
        Page<StopBuildDomain> page = new Page<StopBuildDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (stopBuildVo.getEndDate() != null) {
            stopBuildVo.setEndDate(DateUtils.addDays(stopBuildVo.getEndDate(), 1));
        }
        //查询时，结束日期增加一天
        if (stopBuildVo.getPlanEndDate() != null) {
            stopBuildVo.setPlanEndDate(DateUtils.addDays(stopBuildVo.getPlanEndDate(), 1));
        }
        stopBuildVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = stopBuildService.selectMyCheckByDomainWhere(page, stopBuildVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 停工审批-新增
     * @return
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        //查询开工审批的记录
        StartBuild startBuild = new StartBuild();
        startBuild.setProcessStatus("9");
        List<StartBuild> startBuilds = startBuildService.selectByEntityWhere(startBuild);
        request.setAttribute("startBuilds", startBuilds);
        return "ic/lay-stopBuild-add";
    }

    /**
     * 根据id查询停工审批详情  > 注意：方法命名必须为：toDealOrDetail
     * @param id 停工审批Id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        StopBuildVo stopBuildVo = stopBuildService.queryById(id);
        Map<String, Object> whereMap = Maps.newHashMap();
        whereMap.put("proCode", stopBuildVo.getProCode());
        whereMap.put("processStatus", "9");
        whereMap.put("orderCondition", "ADD_TIME desc");
        List<StartBuild> startBuilds = startBuildService.selectByMapWhere(whereMap);
        if(startBuilds != null && startBuilds.size() > 0){
            StartBuild startBuild = startBuilds.get(0);
            if(startBuild != null){
                CoopInfo coopInfo = new CoopInfo();
                coopInfo.setState(1);
                coopInfo.setId(startBuild.getSgCoopId());
                List<CoopInfo> coopInfos = coopInfoService.selectByEntityWhere(coopInfo);
                if(coopInfos != null && coopInfos.size() > 0){
                    CoopInfo coopInfo2 = coopInfos.get(0);
                    if(coopInfo2 != null){
                        stopBuildVo.setSgCoopName(coopInfo2.getCoopName());
                    }
                }
                stopBuildVo.setInnerSignFlag(startBuild.getInnerSignFlag());
            }
        }
        request.setAttribute("modelDetail", stopBuildVo);
        map.put("stopBuildVo", stopBuildVo);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, stopBuildVo.getProcessId());
        return new ModelAndView("ic/lay-stopBuild-detail", map);
    }

    /**
     * 新增停工审批
     * @return
     */
    @SuppressWarnings({"rawtypes"})
    @RequestMapping("/save")
    public @ResponseBody RspVo save(StopBuildAssist stopBuildAssist) {

        //停工审批表处理
        stopBuildAssist.setHrDeptId(stopBuildAssist.getHrDeptid());
        stopBuildAssist.setHrOrgId(stopBuildAssist.getHrOrgid());
        stopBuildAssist.setStaffName(stopBuildAssist.getStaffname());
        stopBuildAssist.setHrStaffApplyId(stopBuildAssist.getHrStaffApplyid());
        String proCodeAndCodes = stopBuildAssist.getProCodeAndCodes();
        String[] split = proCodeAndCodes.split("\\_");
        stopBuildAssist.setProCode(split[0]);
        stopBuildAssist.setProName(split[2]);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        stopBuildAssist.setId(id);
        stopBuildAssist.setAddBy(vo.getId());
        stopBuildAssist.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String code = codeGeneratorConfigService.generatorCode("ic_stop_build");
        stopBuildAssist.setCodes(code);
        // 处理processId
        stopBuildAssist.setProcessId(IdFactory.getUUIDUpper());
        stopBuildAssist.setProcessStatus("1");//审批中
        stopBuildAssist.setUptBy(vo.getId());
        stopBuildAssist.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        stopBuildService.saveStopBuild(stopBuildAssist);
        LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD,
            "投建管理 -停工审批：项目名=" + stopBuildAssist.getProName(), null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 根据id查询开工审批记录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/getStopBuildById")
    @ResponseBody
    public Map<String, Object> getStopBuildById(HttpServletRequest request) {
        boolean flag = true;
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String proCodeAndCodes = request.getParameter("proCodeAndCodes");
            String[] split = proCodeAndCodes.split("\\_");
            StartBuildVo startBuildVo = startBuildService.queryById(split[1]);
            Map<String, Object> whereMap = Maps.newHashMap();
            whereMap.put("proCode", startBuildVo.getProCode());
            whereMap.put("processStatus", "9");
            whereMap.put("orderCondition", "ADD_TIME desc");
            List<BuildInfo> buildInfos = buildInfoService.selectByMapWhere(whereMap);
            if (buildInfos != null && buildInfos.size() > 0) {
                BuildInfo buildInfo = buildInfos.get(0);
                if (buildInfo != null) {
                    String id = buildInfo.getId();
                    BuildInfoDomain buildInfoDomain = buildInfoService.selectByStopBuild(id);
                    startBuildVo.setBuildLicenceDate(buildInfoDomain.getBuildLicenceDate());
                    startBuildVo.setBuildEndDate(buildInfoDomain.getEndDate());
                }
            }
            map.put("startBuildVo", startBuildVo);
        }
        catch (Exception e) {
            log.error("查询开工审批记录异常", e);
            flag = false;
        }
        map.put("flag", flag);
        return map;
    }

}